package cn.shenlong.portal.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Event;
import cn.shenlong.portal.domain.EventImg;
import cn.shenlong.portal.service.EventService;


@Controller
@RequestMapping("/event/admin")
public class EventAController {

	@Autowired
	private EventService eventService;
	
	
	@RequestMapping("/list")
	public String list(String searchProperty, String searchValue,Date  time,
			Integer pageNumber, Integer pageSize, HttpServletRequest request,
			ModelMap model) {
		if (pageNumber == null) {
			pageNumber = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		Map<String,Object> parm = new HashMap<String,Object>();
		if(searchProperty!=null){
			parm.put(searchProperty, searchValue);
			model.put("searchValue", searchValue);
			parm.put("searchProperty", searchProperty);
			model.put("searchProperty", searchProperty);
		}
		parm.put("time", time);
		parm.put("order", 1);
		Page p = eventService.findPageByParm(parm, pageNumber,
				pageSize);
		model.put("page", p);
		model.put("time", time);
 

		return "/server/event/list";
	} 
	@RequestMapping("/addEvent")
	public String addEvent() {
		return "/server/event/add";
	}

	@RequestMapping("/saveEvent")
	public String saveEvent(Event event,String[] urls,String[] descs, ModelMap model) {
		if(event.getReleaseTime()==null){
			event.setReleaseTime(new Date());
		}
		Integer id = eventService.save(event);
		eventService.saveImgs(urls, descs, id);
		model.put("msg", "success");
		return "redirect:list.html";

	}
	
	@RequestMapping("/editEvent")
	public String editUser(Integer eventId, ModelMap model) {
		Event event = eventService.findByPk(eventId);
		Map<String,Object>  otherParm = new HashMap<String,Object>();
		otherParm.put("eventId", eventId);
		List<EventImg> imgList = eventService.findImgs(otherParm, null);
		model.put("event", event);
		model.put("eventId", eventId);
		model.put("imgList", imgList);
		return "/server/event/edit";
	}

	@RequestMapping("/updateEvent")
	public String updateUser(Event event,String[] urls,String[] descs, ModelMap model) {
		eventService.update(event);
		eventService.deleteByEventId(event.getId());
		eventService.saveImgs(urls, descs, event.getId());
		return "redirect:list.html";
	}
	@RequestMapping("deleteEvent")
	@ResponseBody
	public Map<String, Object> deleteEvent( String eventIdStr,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (eventIdStr != null) {

				String[] tmp = eventIdStr.split(",");
				Integer[] eventIds = new Integer[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					eventIds[i] = Integer.parseInt(tmp[i]);
					eventService.deleteByEventId(eventIds[i]);
				}
				eventService.deleteByIds(eventIds);
			}  
			resultMap.put("result", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}
}
